package com.study.queue;

import java.util.Scanner;

/**
 * 使用数组模拟队列11
 *
 */
public class ArrayQueue {

    public static void main(String[] args) {
        Boolean loop = true;
        Queue queue = new Queue(3);

        while (loop){
            System.out.println("g: getElement");
            System.out.println("a: addElement");
            System.out.println("s: showQueue");
            System.out.println("h: showHead");
            System.out.println("e: exitProgram");
            Scanner scanner = new Scanner(System.in);
            String key = scanner.next();

            switch (key){
                case "g":
                    try {
                        System.out.println(queue.getElement());
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    break;
                case "a":
                    System.out.println("please input the element that you want");
                    int element = scanner.nextInt();
                    try {
                        queue.addElement(element);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    break;
                case "s":
                    queue.showQueue();
                    break;
                case "h":
                    queue.showHead();
                    break;
                case "e":
                    loop =false;
                    System.out.println("program is exit success");
                    break;
            }
        }
    }
}
class Queue{
    private final int maxSize;

    private  int front =-1;

    private int rear = -1;

    private final int arr[];

    public Queue(int maxSize){
        this.maxSize = maxSize;
        arr = new int[this.maxSize];
    }

    public Boolean isNull(){
        return this.front==this.rear;
    }
    public void addElement (int element) {
        if (rear == maxSize -1){
             throw new RuntimeException("Queue can not add because this queue is full");
        }
        rear ++;
        arr[rear] = element;
    }
    public int getElement(){
        if(this.isNull()){
            throw  new RuntimeException("Queue can not get element because this queue is null");
        }
        front++;
        return arr[front];
    }
    public void showQueue(){
        for (int element : this.arr){
            System.out.println(element);
        }
    }

    public void showHead(){
        System.out.println(arr[front+1]);
    }

}